package com.xxl.job.client.impl;

import com.xxl.job.client.AbstractTaskClient;
import com.xxl.job.client.TaskClient;
import com.xxl.job.core.biz.model.ParamInfo;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.biz.model.TaskInfo;
import com.xxl.job.core.util.XxlJobRemotingUtil;
import com.xxl.job.exception.JobException;
import groovy.util.logging.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 定时任务api实现
 *
 * @author huangtao
 * @date 2020/7/3 18:36
 */
@Service
@Slf4j
public class TaskClientImpl extends AbstractTaskClient implements TaskClient {

    @Override
    public int createTask(TaskInfo param) {

        super.validate(param);

        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_CREATE), getToken(), new ParamInfo().build(param), TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("create task error：" + r.getMsg());
        }
        return Integer.valueOf(r.getContent());
    }

    @Override
    public void createAndStart(List<TaskInfo> params) {
        List<ParamInfo> infos = new ArrayList<>();
        for (TaskInfo taskInfo : params) {
            ParamInfo build = new ParamInfo().build(taskInfo);
            super.validate(build);
            infos.add(build);
        }
        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_CREATE_START_BATCH), getToken(), infos, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("create task error：" + r.getMsg());
        }
    }

    @Override
    public int createAndStart(TaskInfo param) {

        super.validate(param);

        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_CREATE_START), getToken(), new ParamInfo().build(param), TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("create task error：" + r.getMsg());
        }
        return Integer.valueOf(r.getContent());
    }


    @Override
    public int createTask(ParamInfo param) {

        super.validate(param);

        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_CREATE), getToken(), param, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("create task error：" + r.getMsg());
        }
        return Integer.valueOf(r.getContent());
    }

    @Override
    public void createTask(List<TaskInfo> param) {
        List<ParamInfo> infos = new ArrayList<>();
        for (TaskInfo taskInfo : param) {
            ParamInfo build = new ParamInfo().build(taskInfo);
            super.validate(build);
            infos.add(build);
        }
        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_CREATE_BATCH), getToken(), infos, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("create task error：" + r.getMsg());
        }
    }


    @Override
    public void startTask(String groupId) {
        TaskInfo requestObj = new TaskInfo(groupId, null);
        super.validate(requestObj);

        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_START), getToken(), requestObj, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("start task error：" + r.getMsg());
        }
    }

    @Override
    public void startTask(String groupId, List<String> includeHandler) {
        List<TaskInfo> infos = new ArrayList<>();
        Set<String> set = new HashSet<>(includeHandler);
        for (String h : set) {
            TaskInfo requestObj = new TaskInfo(groupId, h);
            infos.add(requestObj);
            super.validate(requestObj);
        }
        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_START_LIST), getToken(), infos, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("start task error：" + r.getMsg());
        }
    }

    @Override
    public void startTask(String groupId, String excludeHandler) {
        List<TaskInfo> infos = new ArrayList<>();
        TaskInfo requestObj = new TaskInfo(groupId, excludeHandler);
        infos.add(requestObj);
        super.validate(requestObj);
        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_START_LIST), getToken(), infos, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("start task error：" + r.getMsg());
        }
    }

    @Override
    public void removeTask(String groupId, String handler) {
        TaskInfo requestObj = new TaskInfo(groupId, handler);
        super.validate(requestObj);

        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_DELETE), getToken(), requestObj, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("delete task error：" + r.getMsg());
        }
    }

    @Override
    public void removeTask(String groupId) {
        if (StringUtils.isEmpty(groupId)) {
            throw new JobException("stop task error：groupId is null" );
        }
        TaskInfo requestObj = new TaskInfo(groupId, null);

        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_DELETE), getToken(), requestObj, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("delete task error：" + r.getMsg());
        }
    }

    @Override
    public void updateTask(List<TaskInfo> params) {
        List<ParamInfo> infos = new ArrayList<>();
        for (TaskInfo taskInfo : params) {
            ParamInfo build = new ParamInfo().build(taskInfo);
            super.validate(build);
            infos.add(build);
        }

        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_UPDATE), getToken(), infos, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("update task error：" + r.getMsg());
        }
    }

    @Override
    public void updateTask(TaskInfo info) {
        List<ParamInfo> infos = new ArrayList<>();
        super.validate(info);
        infos.add(new ParamInfo().build(info));
        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_UPDATE), getToken(), infos, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("update task error：" + r.getMsg());
        }
    }

    @Override
    public void stopTask(String groupId, String handler) {
        TaskInfo requestObj = new TaskInfo(groupId, handler);
        super.validate(requestObj);

        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_STOP), getToken(), requestObj, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("stop task error：" + r.getMsg());
        }
    }

    @Override
    public void stopTask(String groupId) {
        if (StringUtils.isEmpty(groupId)) {
            throw new JobException("stop task error：groupId is null" );
        }
        TaskInfo requestObj = new TaskInfo(groupId, null);
        ReturnT<String> r = XxlJobRemotingUtil.postBody(getUri(URL_STOP), getToken(), requestObj, TIMEOUT);
        if (ReturnT.FAIL_CODE == r.getCode()) {
            throw new JobException("stop task error：" + r.getMsg());
        }
    }
}
